package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.ICategoryService;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author mr.meng
 * @since 2025-10-16
 */
@Service
@Slf4j
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 分页查询
     * @param dto
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO dto) {

        //1.调用分页插件的方法进行拦截
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //2.查询数据列表
        List<Category> categoryList = categoryMapper.queryList(dto);
        Page<Category> page = (Page<Category>)categoryList;
        //3.封装分页响应对象
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    /**
     * 新增接口
     * @param dto
     */
    @Override
    public void add(CategoryDTO dto) {

        //将dto对象复制为表实对象
        Category category = BeanUtil.toBean(dto,Category.class);
        //构建分类表实体对象(补充更多属性的值)
        category.setStatus(StatusConstant.DISABLE);//默认禁用
        category.setCreateTime(LocalDateTime.now());//创建时间
        category.setUpdateTime(LocalDateTime.now());//更新时间
        category.setCreateUser(BaseContext.getCurrentId());//创建用户ID
        category.setUpdateUser(BaseContext.getCurrentId());//更新用户ID
        //新增分类表的数据
        categoryMapper.insert(category);
        log.info("[新增分类]新增完毕，分类ID:{}",category.getId());
    }

    /**
     * 修改分类
     *
     * @param dto
     */
    @Override
    public void update(CategoryDTO dto) {
        Category category = new Category();
        BeanUtil.copyProperties(dto,category);

        //设置修改时间、修改人
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.update(category);
    }

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 删除分类
     *
     * @param id
     */
    @Override
    public Result deleteById(Long id) {
        //查询是否关联菜品
        Long countForDish = dishMapper.selectByCateId(id);
        if(countForDish>0){
            return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //查询是否关联套餐
        Long countForSetmeal = setmealMapper.selectByCateId(id);
        if(countForDish>0){
            return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 启用、禁用分类
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Category category = Category.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();
        categoryMapper.update(category);
    }

    /**
     * 根据类型查询
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }
}
